package com.android.car_bluetooth_audio.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;


import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


import com.android.car_bluetooth_audio.R;
import com.android.car_bluetooth_audio.utils.DensityUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class VoiceChannelView extends View {


    private int defaultLineColor = Color.parseColor("#C8C9CC");
    //    private int chooseLineColor = Color.parseColor("#396AFF");//C8C9CC
    private int defaultLineWidth;
    private int defaultMax = 3;
    private int defaultCircleColor = Color.WHITE;
    private int defaultCircleRadius;
    // 当前所在位置
    private int currentProgress;

    // 默认位置
    private int defaultPosition = 8;

    // 一共有多少格
    private int max = 16;
    // 线条颜色
    private int lineColor = Color.parseColor("#C8C9CC");
    private int chooseLineColor = Color.parseColor("#396AFF");
    // 线条粗细
    private int lineWidth;
    private int mHlineWidth;
    //字体颜色
    private int textColor = Color.BLACK;
    //字体大小
    private int smallSize = 14;
    private int standerSize = 16;
    private int bigSize = 28;
    // 圆半径
    private int circleRadius;
    private int circleColor = Color.RED;
    // 一段的宽度，根据总宽度和总格数计算得来
    private int itemWidth;
    private int itemHeight;
    // 控件的宽高
    private int height;
    private int width;
    // 画笔
    private Paint mLinePaint;
    private Paint mHLinePaint;
    private Paint mCirclePaint;

    private float currentY =0;
    // 有效数据点
    private List<Point> points = new ArrayList<>();
    private List<Point> circlePoints = new ArrayList<>();
    //    private float circleY;
    private float circleX;
    private Timer mTestTimer;
    private boolean isSend;

    public  VoiceChannelView(Context context) {
        this(context, null);
    }

    public  VoiceChannelView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        // initDefault
        defaultLineWidth = DensityUtils.dp2px(context, 2);
        defaultCircleRadius = DensityUtils.dp2px(context, 35);
        lineWidth = DensityUtils.dp2px(context, 5);
        mHlineWidth = DensityUtils.dp2px(context, 2);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FontSizeView);
        final int N = typedArray.getIndexCount();
        for (int i = 0; i < N; i++) {
            initCustomAttr(typedArray.getIndex(i), typedArray);
        }
        typedArray.recycle();
        // 初始化画笔
        mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLinePaint.setColor(lineColor);
        mLinePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mLinePaint.setStrokeWidth(lineWidth);

        mHLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mHLinePaint.setColor(lineColor);
        mHLinePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mHLinePaint.setStrokeWidth(mHlineWidth);

        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setColor(circleColor);
        mCirclePaint.setStyle(Paint.Style.FILL);

        // 设置阴影效果
        setLayerType(LAYER_TYPE_SOFTWARE, null);


    }
        private void initCustomAttr(int attr, TypedArray typedArray) {
        if (attr == R.styleable.FontSizeView_lineColor) {
            lineColor = typedArray.getColor(attr, defaultLineColor);
        } else if (attr == R.styleable.FontSizeView_circleColor) {
            circleColor = typedArray.getColor(attr, defaultCircleColor);
        } else if (attr == R.styleable.FontSizeView_lineWidth) {
            lineWidth = typedArray.getDimensionPixelSize(attr, defaultLineWidth);
        } else if (attr == R.styleable.FontSizeView_circleRadius) {
            circleRadius = typedArray.getDimensionPixelSize(attr, defaultCircleRadius);
        }else if (attr == R.styleable.FontSizeView_totalCount) {
            max = typedArray.getInteger(attr, defaultMax);
        } else if (attr == R.styleable.FontSizeView_textFontColor) {
            textColor = typedArray.getColor(attr, textColor);
        } else if (attr == R.styleable.FontSizeView_smallSize) {
            smallSize = typedArray.getInteger(attr, smallSize);
        } else if (attr == R.styleable.FontSizeView_standerSize) {
            standerSize = typedArray.getInteger(attr, standerSize);
        } else if (attr == R.styleable.FontSizeView_bigSize) {
            bigSize = typedArray.getInteger(attr, bigSize);
        }else if (attr == R.styleable.FontSizeView_defaultPosition) {
            defaultPosition = typedArray.getInteger(attr, defaultPosition);
            currentProgress=defaultPosition;
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.e("高度h是多少", String.valueOf(h));
        height = h;
        width = w;
//        circleX = height / 2;
        circleX = width/2;
        // 横线宽度是总宽度-2个圆的半径
//        itemWidth = (w -8 * circleRadius) / max;
        itemHeight = (h-20-2*circleRadius) / max;

        Log.e("itemHeight", String.valueOf(itemHeight));
//        L.info("itemwhid是几文多", String.valueOf(itemWidth)+"是多少");
//        L.info("圆的半径", String.valueOf(circleRadius));
        // 把可点击点保存起来
        Log.e("高度是多少", String.valueOf(height));
        Log.e("cir半径", String.valueOf(circleRadius));
        for (int i = 0; i <=max; i++) {
            int y =i * itemHeight+20+circleRadius;// 3*circleRadius+   +i * circleRadius
//            Log.e("高读","y的高度"+y);
            int x = width / 2;
//            L.info("点x的大小", String.valueOf(x));
            points.add(new Point(x, y));
        }

        circlePoints.addAll(points);
        //初始刻度
        Log.e("默认位置", String.valueOf(defaultPosition));
        currentY = circlePoints.get(defaultPosition).y;
        Log.e("当前的y", String.valueOf(currentY));

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawLine(width/2,circleRadius , width/2, points.get(points.size()-1).y+15, mLinePaint);
        // 绘制刻度
        for (int i=0;i<=max;i++){
//            Log.e("数量", String.valueOf(i%4));
            if (i%4==0){
                canvas.drawLine(points.get(i).x-50,points.get(i).y,points.get(i).x+50,points.get(i).y,mHLinePaint);
            }else{
                canvas.drawLine(points.get(i).x-20,points.get(i).y,points.get(i).x+20,points.get(i).y,mHLinePaint);
            }
        }

        // 画圆
//        Log.e("dd", String.valueOf(currentY < circleRadius));
        if (currentY < circleRadius) {
            currentY = circleRadius;
        }
        if (currentY > height - circleRadius) {
            currentY = height - circleRadius;
        }
//
//        // 实体圆
        canvas.drawCircle(circleX, currentY, circleRadius, mCirclePaint);


    }


    private Point getNearestPoint(float y) {
        Log.e("点y的高度", String.valueOf(y));
        if (height<y){
            Point point = circlePoints.get(circlePoints.size()-1);
            currentProgress=circlePoints.size()-1;
            return point;
        }
        if (itemHeight>y){
            Point point = circlePoints.get(0);
            currentProgress=0;
            return point;
        }
        for (int i = 0; i < circlePoints.size(); i++) {
            Point point = circlePoints.get(i);
//            L.info("点的位置", String.valueOf(point.x));
//            L.info("触摸x的位置",x+"");
//            L.info("itemWidth",itemWidth+"");
//            L.info("point.x - x", String.valueOf(point.x - x));
//            L.info("itemWidth的一半", String.valueOf(itemWidth/2));
            Log.e("点的高度", String.valueOf(point.y));
            Log.e("高度", String.valueOf(point.y-y));
            Log.e("i值怎样", String.valueOf(i));
            Log.e("item的高度", String.valueOf(itemHeight));
            Log.e("item的高度", String.valueOf(itemHeight+circleRadius));
            if (Math.abs(point.y - y) <= itemHeight / 2+circleRadius/2) {
//                L.info("进入到这里吗");
                currentProgress = i;
                return point;
            }
//            if (Math.abs(point.y - y) <= itemHeight+circleRadius+20) {
////                L.info("进入到这里吗");
//                Log.e("i是多少", String.valueOf(i));
//                currentProgress = i;
//                return point;
//            }
        }
        return null;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        currentY = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTestTimer = new Timer();
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                if (!isSend){
                    isSend=true;
                    if (mTestTimer!=null){
                        mTestTimer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                 getNearestPoint(currentY);//566
                                if (onChangeCallbackListener != null) {
                                    Log.e("移动的y","谁叫我");
//                                    onChangeCallbackListener.moveListener(currentProgress);
                                    onChangeCallbackListener.onChangeListener(currentProgress);

                                }
                            }
                        },500);
                    }
                }
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                //回到最近的一个刻度点
                Log.e("当前currentY", String.valueOf(currentY));
                Point targetPoint = getNearestPoint(currentY);//566
                if (targetPoint != null) {
                    // 最终
                    currentY = circlePoints.get(currentProgress).y;
                    invalidate();
                }
                Log.e("奇美拉","lsdw");
                if(mTestTimer!=null)
                {
                    mTestTimer.cancel();
                    mTestTimer.purge();
                }
                isSend=false;
                if (onChangeCallbackListener != null) {
                    Log.e("奇美拉摩擦拉了","lsdw商务网"+currentProgress);
                    onChangeCallbackListener.onChangeListener(currentProgress);
                }

                break;
        }
        return true;
    }



    public void setChangeCallbackListener(OnChangeCallbackListener listener) {
        this.onChangeCallbackListener = listener;
    }

    private OnChangeCallbackListener onChangeCallbackListener;

    public interface OnChangeCallbackListener {
        void onChangeListener(int position);
        void moveListener(int position);
    }
    public void setDefaultPosition(int position){
        Log.e("chognxingha", String.valueOf(position));
        defaultPosition=position;
        currentProgress=defaultPosition;
        if (defaultPosition>=circlePoints.size()){
            currentY = circlePoints.get(circlePoints.size()-1).y;
        }else {
            currentY = circlePoints.get(defaultPosition).y;
        }

        Log.e("defalu", String.valueOf(defaultPosition));
        if (onChangeCallbackListener != null) {
            onChangeCallbackListener.onChangeListener(defaultPosition);
        }
        invalidate();
    }

    public void setDefaultPositionNoListener(int position){
        Log.e("chognxingha", String.valueOf(position));
        defaultPosition=position;
        currentProgress=defaultPosition;
        if (defaultPosition>=circlePoints.size()){
            currentY = circlePoints.get(circlePoints.size()-1).y;
        }else {
            currentY = circlePoints.get(defaultPosition).y;
        }

        Log.e("defalu", String.valueOf(defaultPosition));
//        if (onChangeCallbackListener != null) {
//            onChangeCallbackListener.onChangeListener(defaultPosition);
//        }
        invalidate();
    }

}
